package alexeyk.spring.extjs;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import alexeyk.spring.extjs.models.JExtTheme;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

@Controller
public class HomeController
{

    private static final Logger logger = LoggerFactory.getLogger(HomeController.class);

    @RequestMapping(value = "/getThemes", method = RequestMethod.GET)
    public @ResponseBody
    Object home(HttpServletRequest request)
    {
        List<JExtTheme> result = new ArrayList<JExtTheme>();
        Gson gson = new GsonBuilder().create();
        try
        {
            String realPath = request.getServletContext().getRealPath("/extjs");
            File file = new File(realPath, "/themes");
            for (File theme : file.listFiles())
            {
                JExtTheme extTheme = new JExtTheme(theme.getName());
                result.add(extTheme);
            }
            System.out.println();
        }
        catch (Throwable e)
        {
            e.printStackTrace();
        }
        return gson.toJson(result);
    }
}
